package com.cn.algorithm.two.union;

public class UnionFind_QU extends UnionFind{



    public UnionFind_QU(int capacity) {
        super(capacity);
    }

    @Override
    int find(int v) {
        checkIndex(v);
        while (parent[v] != v) {
            v = parent[v];
        }
        return parent[v];
    }

    @Override
    void union(int v1, int v2) {
        int k1 = find(v1);
        int k2 = find(v2);
        if (k1 == k2) {
            return;
        }
        parent[k1] = k2;
    }
}
